﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Skins;
using DevExpress.LookAndFeel;
using DevExpress.UserSkins;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraBars.Helpers;
using CNotebook;

namespace NotebookS2
{
    public partial class PaymentMeans : RibbonForm
    {
        public PaymentMeans()
        {
            InitializeComponent();
        }

        public bool IsCancelled { get; set; }

        public bool IsReadOnly { get; set; }
        public string DocumentCode { get; set; }
        public string ConnectionString { get; set; }
        public string CashGLAcctCode { get; set; }
        public string CashGLAcctName { get; set; }
        public double CashAmount { get; set; }

        public string CheckGLAcctCode { get; set; }
        public string CheckGLAcctName { get; set; }
        public string CheckBankName { get; set; }
        public string CheckNumber { get; set; }
        public DateTime CheckDate { get; set; }
        public double CheckAmount { get; set; }
        public string CheckStatus { get; set; }

        public string FundTransferAcctCode { get; set; }
        public string FundTransferAcctName { get; set; }
        public string FundTransferBank { get; set; }
        public string FundTransferReference { get; set; }
        public DateTime FundTransferDate { get; set; }
        public double FundTransferAmount { get; set; }


        public DataTable CheckLineItems { get; set; }


        void BindCheckBanks()
        {
            BankManager oManager = new BankManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = new DataTable();

            oTable = oManager.GetBanks();

            

            
            cboBank.DataSource = oTable;
            cboBank.DisplayMember = "BankName";
            cboBank.ValueMember = "BankCode";
            
            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("BankName", "BankName", 100);

            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            cboBank.Columns.Add(col);
            cboBank.ShowHeader = false;
            

            oManager.Close();
        }


        void BindTransferBanks()
        {
            BankManager oManager = new BankManager();
            oManager.ConnectionString = this.ConnectionString;
            oManager.Open();
            DataTable oTable = new DataTable();

            oTable = oManager.GetBanks();




            cboFundTransferBank.Properties.DataSource = oTable;
            cboFundTransferBank.Properties.DisplayMember = "BankName";
            cboFundTransferBank.Properties.ValueMember = "BankCode";

            DevExpress.XtraEditors.Controls.LookUpColumnInfo col;

            col = new DevExpress.XtraEditors.Controls.LookUpColumnInfo("BankName", "BankName", 100);

            col.SortOrder = DevExpress.Data.ColumnSortOrder.Ascending;

            cboFundTransferBank.Properties.Columns.Add(col);
            cboFundTransferBank.Properties.ShowHeader = false;
            cboFundTransferBank.Visible = true;

            oManager.Close();
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.IsCancelled = true;
            this.Close();
        }

        void DisableControls()
        {
            #region Cash Entries
            txtCashGLAcctCode.Properties.ReadOnly = true;
            txtCashGLAcctCode.Properties.Buttons[0].Enabled = false;
            txtCashGLAcctName.Properties.ReadOnly = true;
            txtCashAmount.Properties.ReadOnly = true;
            #endregion

            #region Check Entries
            //txtCheckGLAcctCode.Properties.ReadOnly = true;
            //txtCheckGLAcctCode.Properties.Buttons[0].Enabled = false;
            //txtCheckGLAcctName.Properties.ReadOnly = true;
            //cboCheckBank.Properties.ReadOnly = true;
            //txtCheckNumber.Properties.ReadOnly = true;
            //dtCheckDate.Properties.ReadOnly = true;
            //txtCheckAmount.Properties.ReadOnly = true;
            //txtCheckStatus.Properties.ReadOnly = true;

            gridView1.OptionsBehavior.Editable = false;
            #endregion

            #region Bank Transfer
            txtFundTransferAcctCode.Properties.ReadOnly = true;
            txtFundTransferAcctCode.Properties.Buttons[0].Enabled = false;
            txtFundTransferAcctName.Properties.ReadOnly = true;
            cboFundTransferBank.Properties.ReadOnly = true;
            txtFundTransferRefNum.Properties.ReadOnly = true;
            dtDateTransfered.Properties.ReadOnly = true;
            txtFundTransferedAmt.Properties.ReadOnly = true;
            #endregion
        }

        private void PaymentMeans_Load(object sender, EventArgs e)
        {

            CashReceiptTab.SelectedTabPageIndex = 0;

            if (this.DocumentCode != null)
            {
                if (this.DocumentCode == "Petty Cash")
                {
                    CashReceiptTab.TabPages[0].PageVisible = false;
                    CashReceiptTab.TabPages[1].PageVisible = false;
                    CashReceiptTab.TabPages[2].PageVisible = true;
                }
                else if (this.DocumentCode == "Fund Transfer")
                {
                    CashReceiptTab.TabPages[0].PageVisible = false;
                    CashReceiptTab.TabPages[1].PageVisible = true;
                    CashReceiptTab.TabPages[2].PageVisible = false;
                }
                else if (this.DocumentCode == "Check Voucher")
                {
                    CashReceiptTab.TabPages[0].PageVisible = true;
                    CashReceiptTab.TabPages[1].PageVisible = false;
                    CashReceiptTab.TabPages[2].PageVisible = false;

                }
            }



            gridControl1.DataSource = this.CheckLineItems;
            gridControl1.Refresh();

            if (gridView1.RowCount == 0)
            {
                gridView1.AddNewRow();
                gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["CheckStatus"], "Uncleared");
            }

            BindCheckBanks();
            BindTransferBanks();
            if (this.IsReadOnly == true)
            {
                DisableControls();
            }

            #region Cash Entries
            txtCashGLAcctCode.Text = this.CashGLAcctCode;
            txtCashGLAcctName.Text = this.CashGLAcctName;
            txtCashAmount.Text = Convert.ToDouble(this.CashAmount).ToString();
            #endregion

            //#region Check Entries
            //txtCheckGLAcctCode.Text = this.CheckGLAcctCode.ToString();
            //txtCheckGLAcctName.Text = this.CheckGLAcctName.ToString();
            //cboCheckBank.EditValue = this.CheckBankName.ToString();
            //txtCheckNumber.Text = this.CheckNumber.ToString();
            //dtCheckDate.EditValue = Convert.ToDateTime(this.CheckDate);
            //txtCheckAmount.Text = Convert.ToDouble(this.CheckAmount).ToString();
            //txtCheckStatus.Text = this.CheckStatus.ToString();
            //#endregion

            #region Bank Transfer
            txtFundTransferAcctCode.Text = this.FundTransferAcctCode;
            txtFundTransferAcctName.Text = this.FundTransferAcctName;
            cboFundTransferBank.EditValue = this.FundTransferBank;
            txtFundTransferRefNum.Text = this.FundTransferReference;
            dtDateTransfered.EditValue = Convert.ToDateTime(this.FundTransferDate);
            txtFundTransferedAmt.Text = this.FundTransferAmount.ToString();
            #endregion
        }

        void RemoveBlankLastRowFirst()
        {
            int iCount = -1;
            gridView1.MoveLast();
            try
            {


                iCount = gridView1.FocusedRowHandle;
                string _CHECK_NUMBER = "";


                _CHECK_NUMBER = gridView1.GetRowCellValue(iCount, gridView1.Columns["CheckNumber"]).ToString();

                if (_CHECK_NUMBER.Trim() == "")
                {
                    gridView1.DeleteRow(iCount);
                }
            }
            catch { }

        }

        private void btnOk_Click(object sender, EventArgs e)
        {

            RemoveBlankLastRowFirst();
            this.IsCancelled = false;
            this.CheckAmount = 0;
            try
            {
                foreach (DataRow oRow in this.CheckLineItems.Rows)
                {
                    this.CheckAmount = this.CheckAmount + Convert.ToDouble(oRow["CheckAmount"]);
                }
            }
            catch { }

            #region Cash Entries
            this.CashGLAcctCode = txtCashGLAcctCode.Text;
            this.CashGLAcctName = txtCashGLAcctName.Text;
            this.CashAmount = Convert.ToDouble(txtCashAmount.Text);
            #endregion

            //#region Check Entries
            //this.CheckGLAcctCode = txtCheckGLAcctCode.Text.ToString();
            //this.CheckGLAcctName = txtCheckGLAcctName.Text.ToString();
            //this.CheckBankName = cboCheckBank.EditValue.ToString();
            //this.CheckNumber = txtCheckNumber.Text.ToString();
            //this.CheckDate= Convert.ToDateTime( dtCheckDate.EditValue);
            //this.CheckAmount = Convert.ToDouble(txtCheckAmount.Text);
            //this.CheckStatus = txtCheckStatus.Text.ToString();
            //#endregion

            #region Bank Transfer
            this.FundTransferAcctCode= txtFundTransferAcctCode.Text;
            this.FundTransferAcctName = txtFundTransferAcctName.Text;
            this.FundTransferBank = cboFundTransferBank.EditValue.ToString();
            this.FundTransferReference = txtFundTransferRefNum.Text;
            this.FundTransferDate = Convert.ToDateTime(dtDateTransfered.EditValue);
            this.FundTransferAmount = Convert.ToDouble(txtFundTransferedAmt.Text);
            #endregion

            this.Close();
        }

        private void txtCashGLAcctCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_GLACCOUNT oForm = new FND_GLACCOUNT();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();

        
            txtCashGLAcctCode.Text = oForm.SelectedGLAcctCode;
            txtCashGLAcctName.Text = oForm.SelectedGLAcctName;
        }

        private void txtCheckGLAcctCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_GLACCOUNT oForm = new FND_GLACCOUNT();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();


            //txtCheckGLAcctCode.Text = oForm.SelectedGLAcctCode;
            //txtCheckGLAcctName.Text = oForm.SelectedGLAcctName;
        }

        private void txtFundTransferAcctCode_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            FND_GLACCOUNT oForm = new FND_GLACCOUNT();

            oForm.ConnectionString = this.ConnectionString;
            oForm.ShowDialog();


            txtFundTransferAcctCode.Text = oForm.SelectedGLAcctCode;
            txtFundTransferAcctName.Text = oForm.SelectedGLAcctName;
        }

        private void ribbonControl1_Click(object sender, EventArgs e)
        {

        }

        private void CashReceiptTab_Click(object sender, EventArgs e)
        {

        }

        private void gridView1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                if (MessageBox.Show("Are you sure you want to delete this Item?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.Yes)
                {
                    gridView1.DeleteRow(gridView1.FocusedRowHandle);

                    if (gridView1.RowCount == 0)
                    {
                        gridView1.AddNewRow();
                    }
                }
            }

            if (e.KeyCode == Keys.Down)
            {
                if (gridView1.IsLastRow)
                {
                    if (gridView1.GetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["CheckNumber"]).ToString().Trim() != "")
                    {
                        gridView1.AddNewRow();
                        gridView1.SetRowCellValue(gridView1.FocusedRowHandle, gridView1.Columns["CheckStatus"], "Uncleared");
                    }
                }
            }
        }
    }
}
